package chatRoom2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Sender implements Runnable{
    private DatagramSocket socket;
    private int port;

    public Sender(DatagramSocket socket, int port) {
        this.socket = socket;
        this.port = port;
    }
    @Override
    public void run() {
        Scanner in=new Scanner(System.in);
        while(true){
            try {
                String message=in.nextLine();
                byte[]buf=message.getBytes(StandardCharsets.UTF_8);
                DatagramPacket packet=new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.255"), port);
                socket.send(packet);
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
